package com.cskaoyan.controller.backstagecontroller;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.bean.bo.CreateGoodsBo;
import com.cskaoyan.bean.bo.GoodsOrderReplyBo;
import com.cskaoyan.bean.bo.GoodsRequestParamBo;
import com.cskaoyan.bean.bo.GoodsUpdateBo;
import com.cskaoyan.bean.pojo.Goods;
import com.cskaoyan.bean.pojo.GoodsComment;
import com.cskaoyan.bean.pojo.GoodsProduct;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program:
 * @description:
 * @author: Qian Jin
 * @create: 2021-07-08
 * @detail:
 **/
@RestController
@RequestMapping("admin")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    //评论列表
    @GetMapping("comment/list")
    public BaseRespVo commentList(GoodsRequestParamBo paramBo) {
        GoodsCommentListVo goodsCommentListVo = goodsService.getCommentList(paramBo);
        return BaseRespVo.ok(goodsCommentListVo);
    }

    @PostMapping("order/reply")
    public BaseRespVo orderReply(@RequestBody GoodsOrderReplyBo goodsOrderReplyBo) {
        //没有地方存回复内容
        return BaseRespVo.ok();
    }

    //删除评论
    @PostMapping("comment/delete")
    public BaseRespVo commentDelete(@RequestBody GoodsComment goodsComment) {
        goodsService.deleteComment(goodsComment);
        return BaseRespVo.ok();
    }

    //商品品牌和购物车
    @GetMapping("goods/catAndBrand")
    public BaseRespVo catAndBrand() {
        CatAndBrandVo catAndBrand = goodsService.getCatAndBrand();
        return BaseRespVo.ok(catAndBrand);
    }

    //商品更新
    @PostMapping("goods/update")
    public BaseRespVo goodsUpdate(@RequestBody GoodsUpdateBo goodsUpdateBo) {

        Goods goods = goodsUpdateBo.getGoods();

        if (StringUtils.isEmpty(goods.getGoodsSn()) || StringUtils.isEmpty(goods.getName())) {
            return BaseRespVo.fail("商品编号或商品名称不能为空！");
        }

        if (!goods.getGoodsSn().matches("[0-9]+")) {
            return BaseRespVo.fail("商品编号只能输入数字！");
        }

        int i = goodsService.updateGoods(goodsUpdateBo);

        if (i == 401) {
            return BaseRespVo.fail("商品名重复！请重新修改");
        } else if (i == 402) {
            return BaseRespVo.fail("商品编号重复！请重新修改");
        } else if (i == 500) {
            return BaseRespVo.fail("服务器升级中！修改失败");
        }
        return BaseRespVo.ok();
    }

    //商品详情
    @GetMapping("goods/detail")
    public BaseRespVo goodsDetail(Integer id) {
        GoodsDetailVo goodsDetailVo = goodsService.getGoodsDetail(id);
        return BaseRespVo.ok(goodsDetailVo);
    }

    //商品列表
    @GetMapping("goods/list")
    public BaseRespVo goodsList(GoodsRequestParamBo paramBo) {
        GoodsListVo goodsListVoList = goodsService.getGoodsList(paramBo);
        return BaseRespVo.ok(goodsListVoList);
    }

    //删除商品
    @PostMapping("goods/delete")
    public BaseRespVo goodsDelete(@RequestBody Goods goods) {
        goodsService.deleteGoods(goods);
        return BaseRespVo.ok();
    }

    /*
     *
     * 验证参数：商品编号（不可重复），商品名称（不可重复），同时不能为null
     *
     * */
    //新建商品
    @PostMapping("goods/create")
    public BaseRespVo goodsCreate(@RequestBody CreateGoodsBo createGoodsBo) {

        Goods goods = createGoodsBo.getGoods();

        if (StringUtils.isEmpty(goods.getGoodsSn()) || StringUtils.isEmpty(goods.getName())) {
            return BaseRespVo.fail("商品编号或商品名称不能为空！");
        }

        if (!goods.getGoodsSn().matches("[0-9]+")) {
            return BaseRespVo.fail("商品编号只能输入数字！");
        }

        int i1 = goodsService.existGoodsSn(createGoodsBo);
        if (i1 == 401) {
            return BaseRespVo.fail("商品编号已存在！");
        }

        int i2 = goodsService.existGoodsName(createGoodsBo);
        if (i2 == 402) {
            return BaseRespVo.fail("商品名称已存在！");
        }

        int i = goodsService.createGoods(createGoodsBo);
        if (i == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.fail("创建失败");
        }
    }
}
